package com.ysxsoft.wdg.base;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.classic.common.MultipleStatusView;
import com.ysxsoft.wdg.net.ApiHelper;
import com.ysxsoft.wdg.util.ScreenUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 */
public abstract class BaseFragment extends BaseLazyFragment {
    protected String TAG = this.getClass().getSimpleName();
    protected Context mContext;
    private Unbinder mUnbinder;
    protected ApiHelper mApiHelper;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext = getContext();
        View rootView = LayoutInflater.from(mContext).inflate(getLayoutId(), null);
        mUnbinder = ButterKnife.bind(this, rootView);
        mApiHelper = new ApiHelper();
        initData();
        setListener();
        return rootView;
    }

    protected abstract int getLayoutId();


    protected abstract void initData();
    protected abstract void setListener();


    /**
     * 若fragment需要懒加载操作 重写这个方法
     */
    @Override
    protected void lazyLoadData() {

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
        OkHttpUtils.getInstance().cancelTag(this);
    }

    /**
     *
     * @param topView
     */
    public void initStatusBar(View topView,boolean isWhite) {
        int barHeight = ScreenUtils.getStatusBarHeight(getActivity());
        ViewGroup.LayoutParams params = topView.getLayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//19   4.4
            params.height = barHeight;
            topView.setVisibility(View.VISIBLE);
            topView.setLayoutParams(params);
            if (isWhite){
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {//23
                    topView.setBackgroundColor(Color.parseColor("#40000000"));
                }
            }
        } else {
            params.height = 0;
            topView.setVisibility(View.GONE);
        }
    }

    public void toActivity(Class<?> cls){
        Intent intent = new Intent(getContext(), cls);
        startActivity(intent);
    }
}
